import 'package:enterprise_v6_app/app/components/special_operation/custom_file_upload_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_high_input_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_icon_title_view.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_input_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_button_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_relation_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_work_user_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/common/simple_choice_bean.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_ticket/application/fire_job_application/fire_job_application_controller.dart';

///动火作业-作业申请
class FireJobApplicationPage extends StatelessWidget {
  FireJobApplicationPage({required this.isEnabled, required this.isChange, super.key});

  // 节点是否可编辑
  final bool isEnabled;

  // 是否在交接班中
  final bool isChange;

  final controller = Get.put(FireJobApplicationController());

  /// 获取基本作业信息
  Widget _getBasicJobWidget() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Column(
        children: [
          const CustomIconTitleView(
            title: '作业基本信息',
          ),
          Container(
            color: const Color(0xFFF5F7FC),
            height: 1.w,
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 13.w),
            child: Column(
              children: [
                CustomInputPage(
                  title: '作业编号',
                  isEnabled: isEnabled && !isChange,
                  text: controller.executeTicketReceipt.receiptNo,
                  onCallBack: (value) {
                    controller.executeTicketReceipt.receiptNo = value;
                  },
                ),
                CustomSelectPage(
                    title: '计划作业时间',
                    selectText:
                        '${controller.executeTicketReceipt.ticketPlanList!.planStartDate}\n        至\n${controller.executeTicketReceipt.ticketPlanList!.planEndDate}',
                    isEnabled: false,
                    onCallBack: () {}),
                CustomHighInputPage(
                  title: '作业内容',
                  text: controller.executeTicketReceipt.ticketPlanList!.ticketContent,
                  onCallBack: (value) {},
                  isEnabled: false,
                  isShowDivider: false,
                ),
                CustomInputPage(
                  title: '申请单位',
                  text: controller.executeTicketReceipt.ticketPlanList!.applyDepartmentName,
                  onCallBack: (value) {},
                  isEnabled: false,
                ),
                CustomSelectPage(
                    title: '作业申请时间',
                    isEnabled: isEnabled && !isChange,
                    selectText: controller.executeTicketReceipt.applyDate!,
                    onCallBack: () {
                      controller.showDatePicker();
                    }),
                CustomHighInputPage(
                  title: '动火地点及动火部位',
                  text: controller.executeTicketReceipt.workOtherDataDTO!.workPlace,
                  onCallBack: (value) {
                    controller.executeTicketReceipt.workOtherDataDTO!.workPlace = value;
                  },
                  isEnabled: isEnabled && !isChange,
                  isShowDivider: false,
                ),
                CustomSelectPage(
                    isEnabled: isEnabled && !isChange,
                    selectText: controller.modeConfigChecked,
                    title: '动火方式',
                    hintText: '请选择(多选)',
                    onCallBack: () {
                      controller.showFireWay();
                    }),
                _getFireJobLevelWidget(),
                Divider(
                  height: 1.w,
                  color: AppColors.bg_color_grey,
                ),
                CustomFileUploadPage(
                  isRequired: false,
                  isShowDivider: false,
                  isEnabled: isEnabled && !isChange,
                  uploadedFiles: controller.executeTicketReceipt.workPlan,
                  title: '作业方案：',
                  onCallBack: (value) {
                    controller.executeTicketReceipt.workPlan = value;
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 获取动火作业级别
  Widget _getFireJobLevelWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        AppGaps.vGap16,
        Row(
          children: [
            Text(
              '*',
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                color: AppColors.meaning_color_danger,
              ),
            ),
            Text(
              '动火作业级别',
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.1,
                color: AppColors.font_color_black,
              ),
            )
          ],
        ),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 6.w),
          child: CustomSelectButtonPage<SimpleChoiceBean>(
              isEnabled: isEnabled && !isChange,
              dataList: controller.fireJobLevel,
              onConfirm: (List<SimpleChoiceBean> value) {
                controller.executeTicketReceipt.ticketTypeId = value[0].id;
                controller.executeTicketReceipt.ticketTypeName = value[0].name;
              }),
        ),
      ],
    );
  }

  ///获取作业人信息
  Widget _getWorkUserView() {
    return CustomSelectWorkUserPage(
        title: '动火人',
        isEnabled: isEnabled && !isChange,
        operatorType: 0,
        queryParameters: const {
          'roleName': '作业人',
        },
        dataList: controller.executeTicketReceipt.operatorLists,
        onCallBack: (value) {
          controller.executeTicketReceipt.operatorLists = value;
        });
  }

  ///获取作业负责人信息
  Widget _getUnitLeaderView() {
    return CustomSelectWorkUserPage(
        title: '作业负责人',
        isEnabled: isEnabled && !isChange,
        operatorType: 1,
        isShowCertificateNumber: false,
        queryParameters: const {
          'roleName': '作业负责人',
        },
        dataList: controller.executeTicketReceipt.operatorLists,
        onCallBack: (value) {
          controller.executeTicketReceipt.operatorLists = value;
        });
  }

  // ///获取监护人信息
  // Widget _getGuardianView(){
  //   return CustomSelectWorkUserPage(
  //       title: '监护人',
  //       isEnabled: isEnabled,
  //       operatorType: 2,
  //       isShowCertificateNumber: false,
  //       queryParameters: const {
  //         'roleName': '监护人',
  //       },
  //       dataList: controller.executeTicketReceipt.operatorLists,
  //       onCallBack: (value) {
  //         controller.executeTicketReceipt.operatorLists = value;
  //       });
  // }

  ///获取剩余信息
  Widget _getRemainingView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 13.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Column(
        children: [
          CustomSelectPage(
              isEnabled: isEnabled && !isChange,
              title: '作业单位',
              selectText: controller.executeTicketReceipt.workDepartmentName!,
              // hintText: '请选择(多选)',
              onCallBack: () {
                controller.applyUnitSelect();
              }),
          CustomSelectRelationPage(
            isEnabled: isEnabled && !isChange,
            selectedWork: controller.executeTicketReceipt.otherTicketNo,
            onCallBack: (value) {
              controller.executeTicketReceipt.otherTicketNo = value;
            },
            isShowDivider: false,
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 12.w),
      margin: EdgeInsets.only(bottom: 60.w),
      color: const Color(0xFFF5F5F6),
      alignment: Alignment.topCenter,
      child: GetBuilder(
          init: controller,
          builder: (_) {
            // 防止刷新不生效
            if (controller.executeTicketReceipt.id!.isEmpty) {
              return Container();
            }
            return SingleChildScrollView(
                child: Column(
              children: [
                // 作业基本信息
                _getBasicJobWidget(),
                AppGaps.vGap20,
                // 作业人
                _getWorkUserView(),
                AppGaps.vGap20,
                // 负责人
                _getUnitLeaderView(),
                AppGaps.vGap20,
                // 动火作业监护人在审批节点选择
                // _getGuardianView(),
                // AppGaps.vGap20,
                // 剩余信息
                _getRemainingView(),
              ],
            ));
          }),
    );
  }
}
